/*created by hp
 *on 18-10-12 下午1:31
 */
package com.happts.UI;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import com.happts.Entity.USER;
import com.happts.Service.USERService;

public class SigninUI{
    JTextField jUsernameText;//定义文本框组件
    JPasswordField jPasswordField, jPassConfirm;//定义密码框组件
    JLabel labelUser, labelPass, labelConfirm;
    JPanel jp1,jp2,jp3;
    JButton jb_login, jb_signin; //创建按钮
    ImageIcon login = new ImageIcon("src/com/happts/img/登录.png");
    ImageIcon signin = new ImageIcon("src/com/happts/img/signin.png");
    public SigninUI() {
        JFrame frame = UtilView.createFrame("注册");
        jUsernameText = new JTextField(12);
        jPasswordField = new JPasswordField(12);
        jPassConfirm = new JPasswordField(12);
        labelUser = new JLabel("用户名");
        labelPass = new JLabel("密 码");
        labelConfirm = new JLabel("确认密码");

        jb_signin = new JButton(signin);
        jb_login = new JButton(login);


        //设置布局
        frame.setLayout(null);
        //        frame.setLayout(new GridLayout(3, 2));
        frame.setSize(400, 260);
        frame.setLocation(750, 300);
        int x=50, y=50, w1=30, w2=200, h1= 30;
        labelUser.setBounds(x, y, x, h1);
        jUsernameText.setBounds(x+x+20, y, w2, h1);
        labelPass.setBounds(x, y+h1+10, x, h1);
        jPasswordField.setBounds(x+x+20, y+h1+10, w2, h1);
        labelConfirm.setBounds(x, y+2*h1+25, x, h1);
        jPassConfirm.setBounds(x+x+20, y+2*h1+25, w2, h1);


        jb_signin.setBounds(90, 180, 242, 37);

        frame.add(labelUser);
        frame.add(jUsernameText);//第一块面板添加用户名和文本框

        frame.add(labelPass);
        frame.add(jPasswordField);//第二块面板添加密码和密码输入框
        frame.add(labelConfirm);
        frame.add(jPassConfirm);
        frame.add(jb_login);
        frame.add(jb_signin); //第三块面板添加确认和取消

        //        pp3.setLayout(new FlowLayout());  　　//因为JPanel默认布局方式为FlowLayout，所以可以注销这段代码.

        //设置显示

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        frame.setTitle("注册");

        jb_signin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = jUsernameText.getText().trim();
                String password = jPasswordField.getText();
                String confirm = jPassConfirm.getText();
                if (username.equals("")||password.equals("")||confirm.equals("")){
                    JOptionPane.showMessageDialog(frame, "不允许为空", "注册失败", JOptionPane.WARNING_MESSAGE);
                }
                else if(!password.equals(confirm)){
                    jPasswordField.setText("");
                    jPassConfirm.setText("");
                    JOptionPane.showMessageDialog(frame, "两次密码不一致","注册失败",JOptionPane.WARNING_MESSAGE);
                }
                else{
                //查询是否有重复的
                    USERService us = new USERService();
                    List<USER> userList = us.find(username);
                    if(!userList.isEmpty()){
                        jUsernameText.setText("");
                        JOptionPane.showMessageDialog(frame, "用户名已存在","注册失败", JOptionPane.WARNING_MESSAGE);
                    }else{
                        /*
                        * 实例化User, insert，dialog，dispose，initview
                        * */
                        USER user = new USER(username, password);
                        if(us.add(user)){
                            JOptionPane.showMessageDialog(frame, "注册成功","注册成功", JOptionPane.YES_NO_CANCEL_OPTION);
                            frame.dispose();
                            new LoginUI();
                        }else {
                            JOptionPane.showMessageDialog(frame, "注册失败","注册失败", JOptionPane.WARNING_MESSAGE);
                        }

                    }
                }

            }
        });

    }
    public static void main(String[] args){
        new SigninUI();
    }
}
